package com.kimu.user_service_consumer.controller;

import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.kimu.user.entity.User;
import com.kimu.user.service.JwtService;
import com.kimu.user.service.UserService;
import com.nimbusds.jose.JOSEException;

import jakarta.websocket.server.PathParam;
import org.springframework.web.bind.annotation.RequestParam;


@RestController
public class UserController {

    @DubboReference
    private UserService userService;

    private JwtService jwtService;

    public UserController(JwtService jwtService) {
        this.jwtService = jwtService;
    }

    /*
     * 获取某个用户的信息
     */
    @GetMapping(path = "/api/users/{id}")
    public ResponseEntity<User> findUserById(@PathVariable("id") Long id) {
        User user = userService.findUserById(id);
        if (null == user) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        } else {
            return new ResponseEntity<>(user, HttpStatus.OK);
        }
    }

    /*
     * 增加一个用户，要求用户名和密码不能为空
     */
    @PostMapping(path = "/api/users")
    public ResponseEntity<User> createUser(@RequestBody User user) {
        int row = userService.addUser(user);
        if (row == 0) {
            // 如果没有添加成功，则返回错误代码
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(user, HttpStatus.CREATED);
        }
    }

    /*
     * 修改一个用户，只允许部分修改
     */
    @PatchMapping(path = "/api/users/{id}")
    public ResponseEntity<User> updateUserById(@RequestBody User user) {
        // 注意如果不希望user的某些字段变更，请把它们设置成null
        int row = userService.updateUser(user);
        if (row == 0) {
            // 如果没有修改成功，则返回错误代码
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(user, HttpStatus.OK);
        }
    }

    /*
     * 删除一个用户，使用逻辑删除，并非真得把用户删除，需要is_deleted字段的支持
     */
    @DeleteMapping(path = "/api/users/{id}")
    public ResponseEntity<User> deleteUserById(@PathVariable("id") Long id) {
        int row = userService.deleteUserById(id);
        if (row == 0) {
            // 如果没有删除成功，则返回错误代码
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(HttpStatus.NO_CONTENT);
        }
    }

    /*
     * 用户注册，获取验证码，验证码会发送到邮箱中
     */
    @PostMapping(path = "/api/validCode")
    public ResponseEntity<?> createValidCode(@RequestBody User user) {
        String email = user.getEmail();
        int num = userService.createValidCode(email);
        if (num == 0) {
            // 验证码创建失败，则返回错误信息
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(HttpStatus.OK);
        }
    }

    /*
     * 用户注册，注册一个用户，需要用户名、密码、验证码等
     */
    @PostMapping(path = "/api/register")
    public ResponseEntity<User> register(@RequestBody User user, @PathParam("validCode") String validCode) {
        int row = userService.register(user, validCode);
        if (row == 0) {
            // 验证不通过，或者信息不对
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        } else {
            return new ResponseEntity<>(user, HttpStatus.CREATED);
        }
    }

    /*
     * 用户登录，登录成功会返回一个token
     */
    @PostMapping(path = "/api/login")
    public ResponseEntity<User> login(@RequestBody User user) {
        User userFinded = userService.login(user);
        if (userFinded == null) {
            // 用户名和密码验证不通过，禁止登录
            return new ResponseEntity<>(HttpStatus.FORBIDDEN);
        } else {
            return new ResponseEntity<>(userFinded, HttpStatus.OK);
        }
    }

    /*
     * 获取一个用户的jwt
     */
    @GetMapping(path = "/api/jwt")
    public String getMethodName(@RequestParam("username") String username) throws JOSEException {
        String token = jwtService.generateToken(username);
        return token;
    }
    
}
